package com.example.administrator.fragmentwx;


import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;


/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment1 extends Fragment {
    //声明微信列表集合
    private ArrayList<WxBean> wxList;
    private MainActivity activity;
    public Fragment1() {


    }

    @Override
    public void onCreate(@Nullable Bundle sacedInstanceState){
        super.onCreate(sacedInstanceState);
        activity = (MainActivity)getActivity();

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =inflater.inflate(R.layout.fragment_fragment1, container, false);
        wxList = init();
        //找到ListView
        ListView lvMain = view.findViewById(R.id.lv_main);
        //创建适配器
        MyAdapter adapter = new MyAdapter();
        lvMain.setAdapter(adapter);
        return view;

    }

    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return wxList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            WxBean news = wxList.get(position);
            ViewHolder holder = null;
            if (convertView ==null){
                convertView=View.inflate(activity.getApplicationContext(),R.layout.item,null);
                holder = new ViewHolder();
                holder.wpic = convertView.findViewById(R.id.iv_icon);
                holder.wname = convertView.findViewById(R.id.tv_wname);
                holder.wtime = convertView.findViewById(R.id.tv_wtime);
                holder.wsource = convertView.findViewById(R.id.tv_wsource);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder)convertView.getTag();
            }
            holder.wpic.setTag(news.getWpic());
            holder.wname.setTag(news.getWname());
            holder.wtime.setTag(news.getWtime());
            holder.wsource.setTag(news.getWsource());
//            TextView tvTitle = convertView.findViewById(R.id.tv_title);
//            tvTitle.setText("第"+position+"行");
            return convertView;
        }
    }
    private ArrayList<WxBean> init() {
        ArrayList<WxBean> wlist =new ArrayList<WxBean>();
        WxBean news1 = new WxBean(R.drawable.img1,"应管学院团支书集合","18:13","软升本202王莹：收到");
        WxBean news2 = new WxBean(R.drawable.img2,"软升本20级","18:03","班导师：各位老师，辽宁省水资源管理集团是省直单位，有时也转企。");
        WxBean news3 = new WxBean(R.drawable.img3,"22届升本毕业生群","16:43","就业李xx：[链接]燃青春 建未来");
        WxBean news4 = new WxBean(R.drawable.img4,"软升本202班","13:38","软升本202马xx：请同学们填写信息，考公的同学备注一下");
        WxBean news5 = new WxBean(R.drawable.img5,"七楼","10月10日","班导师：各位老师，辽宁省水资源管理集团是省直单位，有时也转企。");
        WxBean news6 = new WxBean(R.drawable.img6,"大外2-20级412","10月8日","骆xx：俄语行吗");
        WxBean news7 = new WxBean(R.drawable.img7,"软升本-数据结构","10月5日","“软升本xx”撤回一条消息");
        WxBean news8 = new WxBean(R.drawable.img8,"软件工程","6月21日","牟x：[文件]");
        WxBean news9 = new WxBean(R.drawable.img9,"英语课","2020年11月19日","马xx：[图片]");
        Collections.addAll(wlist,news1,news2,news3,news4,news5,news6,news7,news8,news9);

        return wlist;
    }

}

